package com.sunny.ynhz.ui.report;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.BottomSheetDialog;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.sunny.ynhz.R;
import com.sunny.ynhz.adapter.CurrentRiverAdapter;
import com.sunny.ynhz.bean.CurrentRiverBean;
import com.sunny.ynhz.bean.WarpRiverBean;
import com.sunny.ynhz.model.RiverMsgModel;
import com.sunny.ynhz.ui.base.AppBaseTranslationTitleBarActivity;
import com.sunny.ynhz.utils.BottomSheetListView;
import com.sunny.ynhz.utils.SPUtil;
import com.sunny.ynhz.utils.ShowUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

import static com.sunny.ynhz.utils.ApplicationUtils.getContext;

/**
 * Created by huhuan123 on 2018/9/5.
 *
 * @describe TODO
 */

public class ChooseRiverActivityNative extends AppBaseTranslationTitleBarActivity implements RiverMsgModel.IRiverModel<WarpRiverBean> {

    private ListView lv_all_river;
    private List<Object> list;
    private BottomSheetDialog bottomSheetDialog;
    private View view_river_child;
    String reachId = "";
    String id = "";
    String name = "";
    private List<Object> mList;
    private EditText ed_search_river;
    private List<Object> allList;
    private TextView tv_choose_area;
    @BindView(R.id.ll_choose)
    LinearLayout ll_choose;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lv_river_msg);
        setTitle("选择河道");
        String userType = SPUtil.get("userType", "").toString();
        if (userType.equals("1") || userType.equals("2") || userType.equals("3") || userType.equals("4") || userType.equals("5") || userType.equals("6")) {
            ll_choose.setVisibility(View.VISIBLE);
        }
        ed_search_river = findViewById(R.id.ed_search_river);
        bottomSheetDialog = new BottomSheetDialog(this);
        RiverMsgModel riverMsgModel = new RiverMsgModel(this, this);
        lv_all_river = findViewById(R.id.lv_current_river);
        tv_choose_area = findViewById(R.id.tv_choose_area);
        riverMsgModel.getCurrentRiverData();
        lv_all_river.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Object object;
                if (TextUtils.isEmpty(ed_search_river.getText().toString()) && TextUtils.isEmpty(tv_choose_area.getTag().toString())) {
                    object = list.get(i);
                } else {
                    object = mList.get(i);
                }

                if (object instanceof CurrentRiverBean.DatecountBean) {
                    CurrentRiverBean.DatecountBean datecountBean = (CurrentRiverBean.DatecountBean) object;
                    List<CurrentRiverBean.DatecountBean> currentSingleDataCountsBean = riverMsgModel.getCurrentSingleDataCountsBean(datecountBean.getID());
                    if (currentSingleDataCountsBean.size() == 0) {
                        String[] split = datecountBean.getPARENTIDS().split(",");
                        reachId = split[0];
                        id = datecountBean.getID();
                        name = datecountBean.getNAME();
                        resultIntent();
                    } else {
                        ArrayList list = new ArrayList();
                        list.add(datecountBean);
                        list.addAll(currentSingleDataCountsBean);
                        disPlayRiverChildMsg(list);

                    }
                } else {
                    CurrentRiverBean.DataBean datecountBean = (CurrentRiverBean.DataBean) object;
                    id = datecountBean.getRIVERID();
                    name = datecountBean.getNAME();
                    reachId = datecountBean.getPARENTID();
                    resultIntent();
                }
            }
        });

        ed_search_river.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                mList = searchCurrentMsg(charSequence.toString(), tv_choose_area.getTag().toString());
                CurrentRiverAdapter currentRiverAdapter = new CurrentRiverAdapter(ChooseRiverActivityNative.this, mList);
                lv_all_river.setAdapter(currentRiverAdapter);
            }

            @Override
            public void afterTextChanged(Editable editable) {

            }
        });
        tv_choose_area.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(ChooseRiverActivityNative.this, ChooseAreaActivity.class);
                intent.putExtra("type", 1);
                startActivityForResult(intent, 500);
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 500 && resultCode == RESULT_OK) {
            String areaCode = data.getStringExtra("areaCode");
            String name = data.getStringExtra("areaName");
            tv_choose_area.setTag(areaCode);
            tv_choose_area.setText(name);
            mList = searchCurrentMsg(ed_search_river.getText().toString(), areaCode);
            CurrentRiverAdapter currentRiverAdapter = new CurrentRiverAdapter(ChooseRiverActivityNative.this, mList);
            lv_all_river.setAdapter(currentRiverAdapter);
        }

    }

    @Override
    public void onSuccess(WarpRiverBean warpRiverBean) {
        ShowUtils.closePopwindow();
        allList = warpRiverBean.getAllList();
        list = new ArrayList<>();
        List<CurrentRiverBean.DataBean> dataBeans = warpRiverBean.getmDataBeans();
        ArrayList<CurrentRiverBean.DatecountBean> mdateCountBeans = warpRiverBean.getMdateCountBeans();
        list.addAll(dataBeans);
        list.addAll(mdateCountBeans);
        CurrentRiverAdapter currentRiverAdapter = new CurrentRiverAdapter(this, list);
        lv_all_river.setAdapter(currentRiverAdapter);
    }

    public void disPlayRiverChildMsg(List<Object> list) {
        view_river_child = LayoutInflater.from(this).inflate(R.layout.view_river_child, null);
        BottomSheetListView bottomSheetListView = view_river_child.findViewById(R.id.lv_btm);
        CurrentRiverAdapter currentRiverAdapter = new CurrentRiverAdapter(this, list);
        bottomSheetListView.setAdapter(currentRiverAdapter);
        bottomSheetDialog.setContentView(view_river_child);
        bottomSheetDialog.show();
        bottomSheetListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                CurrentRiverBean.DatecountBean datecountBean = (CurrentRiverBean.DatecountBean) list.get(i);
                String[] split = datecountBean.getPARENTIDS().split(",");
                reachId = split[0];
                id = datecountBean.getID();
                name = datecountBean.getNAME();
                resultIntent();
            }
        });

    }

    private void resultIntent() {
        Intent intent = new Intent();
        intent.putExtra("reachId", reachId);
        intent.putExtra("id", id);
        intent.putExtra("name", name);
        setResult(600, intent);
        finish();
    }

    @Override
    public void onFail() {
        ShowUtils.closePopwindow();
    }

    @Override
    public void onStartLoad() {

        ShowUtils.inItPopwin(ChooseRiverActivityNative.this, "加载中");


    }

    public List<Object> searchCurrentMsg(String name, String code) {
        if (TextUtils.isEmpty(name) && TextUtils.isEmpty(code)) {
            return list;
        }
        String nameStr;
        String codeStr;
        List<Object> searchList = new ArrayList<>();
        for (int i = 0; i < allList.size(); i++) {
            Object object = allList.get(i);
            if (object instanceof CurrentRiverBean.DatecountBean) {
                CurrentRiverBean.DatecountBean datecountBean = (CurrentRiverBean.DatecountBean) object;
                nameStr = datecountBean.getNAME();
                codeStr = datecountBean.getADDVCDAREA();
            } else {
                CurrentRiverBean.DataBean datecountBean = (CurrentRiverBean.DataBean) object;
                nameStr = datecountBean.getNAME();
                codeStr = datecountBean.getADDVCDAREA();
            }
            if (TextUtils.isEmpty(code) && !TextUtils.isEmpty(nameStr)) {
                if (nameStr.contains(name)) {
                    searchList.add(object);
                }
            }
            if (TextUtils.isEmpty(nameStr) && !TextUtils.isEmpty(code)) {
                if (code.equals(codeStr)) {
                    searchList.add(object);
                }
            }
            if (!TextUtils.isEmpty(nameStr) && !TextUtils.isEmpty(code)) {
                if (nameStr.contains(name) && codeStr.equals(code)) {
                    searchList.add(object);
                }
            }

        }
        return searchList;
    }
}
